﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace BuildColor
{
    public partial class Form1 : Form
    {        
        //屋面
        const string _top = "0,284;0,331;307,415;606,355;69,279;|"+
                            "66,258;605,313;652,298;106,250;|"+
                            "650,316;650,347;689,344;688,319;|"+
                            "437,430;689,491;688,378;";
        //墙面
        const string _wall = "0,331;0,378;307,490;306,414;|"+
                            "0,392;0,397;307,520;307,513;|"+
                            "61,400;61,416;83,425;82,408;|"+
                            "239,465;238,486;307,513;307,490;304,490;304,505;292,501;292,484;282,481;282,497;271,492;271,477;|"+
                            "459,435;481,477;611,514;612,541;497,505;498,507;662,558;662,573;689,581;688,491;|68,258;69,279;607,356;607,313;|"+
                            "306,414;307,522;436,488;496,507;494,504;456,492;457,470;479,475;460,435;442,429;688,378;689,342;650,347;650,299;607,312;607,355;";
        //腰带
        const string _mid = "0,348;0,356;307,448;688,366;688,358;307,440;"+
                            "|436,445;436,453;689,523;689,515;";

        Color _clrTop = Color.Empty;
        Color _clrMid = Color.Empty;
        Color _clrWall = Color.Empty;
        
        public Form1()
        {
            InitializeComponent();
            this.pictureBox1.Image = GetImage();
        }

        Image GetImage()
        {
            return global::BuildColor.Properties.Resources.model;
        }

        void BuilderColor()
        {            
            Image _img = GetImage();
            Graphics _g = Graphics.FromImage(_img);
            _g.SmoothingMode = SmoothingMode.HighQuality;

            string[] _ss = null;
            if (_clrTop != Color.Empty)
            {
                _ss =_top.Replace("\r\n", ";").Trim().Split('|');
                foreach (string s in _ss)
                {
                    DrawColor(s, _clrTop, _g);
                }
            }

            if (_clrWall != Color.Empty)
            {
                _ss = _wall.Replace("\r\n", ";").Trim().Split('|');
                foreach (string s in _ss)
                {
                    DrawColor(s, _clrWall, _g);
                }
            }

            if (_clrMid != Color.Empty)
            {
                _ss = _mid.Replace("\r\n", ";").Trim().Split('|');
                foreach (string s in _ss)
                {
                    DrawColor(s, _clrMid, _g);
                }
            }

            this.pictureBox1.Image = _img;
            _g.Dispose();
        }

        void DrawColor(string loc, Color clr, Graphics g)
        {            
            string[] _ss = loc.Trim().TrimEnd(';').TrimStart(';').Split(';');
            Point[] _pt = new Point[_ss.Length];
            for (int i = 0; i < _ss.Length; i++)
            {
                if (_ss[i] == "")
                    continue;
                _pt[i] = new Point(Convert.ToInt32(_ss[i].Split(',')[0]),
                    Convert.ToInt32(_ss[i].Split(',')[1]));
            }                        
            SolidBrush _sb = new SolidBrush(Color.FromArgb(180, clr));
            g.FillPolygon(_sb, _pt);
            _sb.Dispose();                       
        }

        /// <summary>
        /// 去除原字符串结尾处的所有替换字符串
        /// 如：原字符串"sdlfjdcdcd",替换字符串"cd" 返回"sdlfjd"
        /// </summary>
        /// <param name="strSrc"></param>
        /// <param name="strTrim"></param>
        /// <returns></returns>
        public static string TrimEnd(string strSrc, string strTrim)
        {
            if (strSrc.EndsWith(strTrim))
            {
                string strDes = strSrc.Substring(0, strSrc.Length - strTrim.Length);
                return TrimEnd(strDes, strTrim);
            }
            return strSrc;
        }
         
        void SetMouseState(object sender, MouseEventArgs e, int flag)
        {            
            Color _clr = GetScrPixel(this.PointToScreen(e.Location));
            if (e.X >= 735 && e.X <= 840 && e.Y >= 672 && e.Y <= 694)
            {
                this.Cursor = Cursors.Hand;
                if (flag == 1)              //当鼠标弹起   
                {
                    Application.Exit();
                }                
                return;
            }
            if (e.X >= 856 && e.X <= 962 && e.Y >= 672 && e.Y <= 694)                        
            {
                this.Cursor = Cursors.Hand;
                if (flag == 1)              //当鼠标弹起   
                {                    
                    About _ab = new About();
                    _ab.ShowDialog();
                    this.Cursor = Cursors.Default;
                }
                return;
            }
            if ((e.X >= 15 && e.X <= 84 && e.Y >= 108 && e.Y <= 122) ||
                        (e.X >= 15 && e.X <= 84 && e.Y >= 130 && e.Y <= 144) ||
                        (e.X >= 15 && e.X <= 84 && e.Y >= 152 && e.Y <= 164) ||
                        (e.X >= 15 && e.X <= 84 && e.Y >= 172 && e.Y <= 186) ||
                        (e.X >= 15 && e.X <= 84 && e.Y >= 192 && e.Y <= 207) ||
                        (e.X >= 15 && e.X <= 84 && e.Y >= 214 && e.Y <= 228) ||
                        (e.X >= 15 && e.X <= 84 && e.Y >= 236 && e.Y <= 250) ||
                        (e.X >= 15 && e.X <= 84 && e.Y >= 258 && e.Y <= 272) ||
                        (e.X >= 15 && e.X <= 84 && e.Y >= 280 && e.Y <= 292) ||
                        (e.X >= 15 && e.X <= 84 && e.Y >= 300 && e.Y <= 313) ||
                        (e.X >= 15 && e.X <= 84 && e.Y >= 322 && e.Y <= 334) ||
                        (e.X >= 15 && e.X <= 84 && e.Y >= 342 && e.Y <= 354) ||
                        (e.X >= 15 && e.X <= 84 && e.Y >= 364 && e.Y <= 376) ||
                        (e.X >= 15 && e.X <= 84 && e.Y >= 384 && e.Y <= 398) ||
                        (e.X >= 15 && e.X <= 84 && e.Y >= 406 && e.Y <= 419) ||
                        (e.X >= 15 && e.X <= 84 && e.Y >= 426 && e.Y <= 440) ||
                        (e.X >= 15 && e.X <= 84 && e.Y >= 448 && e.Y <= 462) ||
                        (e.X >= 15 && e.X <= 84 && e.Y >= 470 && e.Y <= 484) ||

                        (e.X >= 100 && e.X <= 170 && e.Y >= 172 && e.Y <= 185) ||
                        (e.X >= 100 && e.X <= 170 && e.Y >= 193 && e.Y <= 206) ||
                        (e.X >= 100 && e.X <= 170 && e.Y >= 214 && e.Y <= 228) ||
                        (e.X >= 100 && e.X <= 170 && e.Y >= 235 && e.Y <= 248) ||
                        (e.X >= 100 && e.X <= 170 && e.Y >= 255 && e.Y <= 270) ||
                        (e.X >= 100 && e.X <= 170 && e.Y >= 278 && e.Y <= 292) ||
                        (e.X >= 100 && e.X <= 170 && e.Y >= 299 && e.Y <= 312) ||
                        (e.X >= 100 && e.X <= 170 && e.Y >= 320 && e.Y <= 333) ||
                        (e.X >= 100 && e.X <= 170 && e.Y >= 342 && e.Y <= 355) ||
                        (e.X >= 100 && e.X <= 170 && e.Y >= 362 && e.Y <= 375) ||
                        (e.X >= 100 && e.X <= 170 && e.Y >= 383 && e.Y <= 397) ||
                        (e.X >= 100 && e.X <= 170 && e.Y >= 406 && e.Y <= 419) ||
                        (e.X >= 100 && e.X <= 170 && e.Y >= 428 && e.Y <= 440) ||
                        (e.X >= 100 && e.X <= 170 && e.Y >= 446 && e.Y <= 461) ||
                        (e.X >= 100 && e.X <= 170 && e.Y >= 469 && e.Y <= 483) ||
                        (e.X >= 100 && e.X <= 170 && e.Y >= 490 && e.Y <= 503) ||
                        (e.X >= 100 && e.X <= 170 && e.Y >= 511 && e.Y <= 524) ||
                        (e.X >= 100 && e.X <= 170 && e.Y >= 532 && e.Y <= 545)
                       )
            {
                this.Cursor = Cursors.Hand;
                if (flag == 1)              //当鼠标弹起    
                {
                    _clrTop = _clr;
                    BuilderColor();
                }
                return;
            }
            if ((e.X >= 93 && e.X <= 122 && e.Y >= 573 && e.Y <= 588) ||
                (e.X >= 135 && e.X <= 164 && e.Y >= 573 && e.Y <= 588) ||
                (e.X >= 180 && e.X <= 207 && e.Y >= 573 && e.Y <= 588) ||
                (e.X >= 223 && e.X <= 248 && e.Y >= 573 && e.Y <= 588) ||
                (e.X >= 265 && e.X <= 290 && e.Y >= 573 && e.Y <= 588) ||
                (e.X >= 306 && e.X <= 331 && e.Y >= 573 && e.Y <= 588) ||
                (e.X >= 347 && e.X <= 373 && e.Y >= 573 && e.Y <= 588) ||
                (e.X >= 389 && e.X <= 415 && e.Y >= 573 && e.Y <= 588) ||
                (e.X >= 431 && e.X <= 455 && e.Y >= 573 && e.Y <= 588) ||
                (e.X >= 471 && e.X <= 500 && e.Y >= 573 && e.Y <= 588) ||

                (e.X >= 93 && e.X <= 122 && e.Y >= 596 && e.Y <= 608) ||
                (e.X >= 135 && e.X <= 164 && e.Y >= 596 && e.Y <= 608) ||
                (e.X >= 180 && e.X <= 207 && e.Y >= 596 && e.Y <= 608) ||
                (e.X >= 223 && e.X <= 248 && e.Y >= 596 && e.Y <= 608) ||
                (e.X >= 265 && e.X <= 290 && e.Y >= 596 && e.Y <= 608) ||
                (e.X >= 306 && e.X <= 331 && e.Y >= 596 && e.Y <= 608) ||
                (e.X >= 347 && e.X <= 373 && e.Y >= 596 && e.Y <= 608) ||
                (e.X >= 389 && e.X <= 415 && e.Y >= 596 && e.Y <= 608) ||
                (e.X >= 431 && e.X <= 455 && e.Y >= 596 && e.Y <= 608) ||
                (e.X >= 471 && e.X <= 500 && e.Y >= 596 && e.Y <= 608)
            )
            {
                this.Cursor = Cursors.Hand;
                if (flag == 1)              //当鼠标弹起    
                {
                    _clrWall = _clr;
                    BuilderColor();
                }
                return;
            }
            if ((e.X >= 225 && e.X <= 256 && e.Y >= 618 && e.Y <= 630))
            {
                this.Cursor = Cursors.Hand;
                if (flag == 1)              //当鼠标弹起 
                {
                    _clrMid = Color.Empty;
                    BuilderColor();
                }
                return;
            }
            if (        (e.X >= 267 && e.X <= 298 && e.Y >= 618 && e.Y <= 630) ||
                        (e.X >= 309 && e.X <= 340 && e.Y >= 618 && e.Y <= 630) ||
                        (e.X >= 351 && e.X <= 382 && e.Y >= 618 && e.Y <= 630) ||
                        (e.X >= 393 && e.X <= 423 && e.Y >= 618 && e.Y <= 630) ||
                        (e.X >= 435 && e.X <= 466 && e.Y >= 618 && e.Y <= 630) ||
                        (e.X >= 476 && e.X <= 507 && e.Y >= 618 && e.Y <= 630) ||
                        (e.X >= 518 && e.X <= 549 && e.Y >= 618 && e.Y <= 630) ||
                        (e.X >= 560 && e.X <= 591 && e.Y >= 618 && e.Y <= 630) ||
                        (e.X >= 601 && e.X <= 632 && e.Y >= 618 && e.Y <= 630) ||

                        (e.X >= 225 && e.X <= 256 && e.Y >= 639 && e.Y <= 652) ||
                        (e.X >= 267 && e.X <= 298 && e.Y >= 639 && e.Y <= 652) ||
                        (e.X >= 309 && e.X <= 340 && e.Y >= 639 && e.Y <= 652) ||
                        (e.X >= 351 && e.X <= 382 && e.Y >= 639 && e.Y <= 652) ||
                        (e.X >= 393 && e.X <= 423 && e.Y >= 639 && e.Y <= 652) ||
                        (e.X >= 435 && e.X <= 466 && e.Y >= 639 && e.Y <= 652) ||
                        (e.X >= 476 && e.X <= 507 && e.Y >= 639 && e.Y <= 652) ||
                        (e.X >= 518 && e.X <= 549 && e.Y >= 639 && e.Y <= 652) ||
                        (e.X >= 560 && e.X <= 591 && e.Y >= 639 && e.Y <= 652) ||
                        (e.X >= 601 && e.X <= 632 && e.Y >= 639 && e.Y <= 652)
                        )
            {
                this.Cursor = Cursors.Hand;
                if (flag == 1)              //当鼠标弹起 
                {
                    _clrMid = _clr;
                    BuilderColor();
                }
                return;
            }
            this.Cursor = Cursors.Default;
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            SetMouseState(sender, e,0);
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            SetMouseState(sender, e, 1);
        }

        static Color GetScrPixel(Point pt)
        {
            var scrBound = Screen.PrimaryScreen.Bounds;
            using (var bmp = new Bitmap(scrBound.Width, scrBound.Height))
            {
                using (var g = Graphics.FromImage(bmp))
                {
                    g.CopyFromScreen(scrBound.Location,
                                        scrBound.Location,
                                        scrBound.Size);
                }
                return bmp.GetPixel(pt.X, pt.Y);
            }
        }
    }
}
